package com.rbac.api.mybatis.mapper;

import com.rbac.api.controller.request.PostUsersListRequest;
import com.rbac.api.mybatis.entity.AdminUsersEntity;
import com.rbac.api.mybatis.sqlProvider.AdminUsersSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface AdminUsersMapper {



    ///登录（根据账号查询）
    @Select({
            "select",
            "id, user_no, user_account, user_name, user_pwd, user_salt, user_emali, user_mobile, ",
            "user_avatar, user_address, user_sex, last_login_time, last_login_ip, user_status, ",
            "create_uid, create_time, last_update_uid, last_update_time",
            "from admin_users",
            "where user_account = #{account,jdbcType=VARCHAR} limit 1"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="user_no", property="userNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_account", property="userAccount", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_pwd", property="userPwd", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_salt", property="userSalt", jdbcType=JdbcType.CHAR),
            @Result(column="user_emali", property="userEmali", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_mobile", property="userMobile", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_avatar", property="userAvatar", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_address", property="userAddress", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_sex", property="userSex", jdbcType=JdbcType.INTEGER),
            @Result(column="last_login_time", property="lastLoginTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="last_login_ip", property="lastLoginIp", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_status", property="userStatus", jdbcType=JdbcType.INTEGER),
            @Result(column="create_uid", property="createUid", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="last_update_uid", property="lastUpdateUid", jdbcType=JdbcType.VARCHAR),
            @Result(column="last_update_time", property="lastUpdateTime", jdbcType=JdbcType.TIMESTAMP)
    })
    AdminUsersEntity selectLoginByUserAccount(@Param("account") String account);



    ///管理员修改密码
    @Update({
            "update admin_users",
            "set user_pwd = #{userPwd,jdbcType=VARCHAR},",
            "user_salt = #{userSalt,jdbcType=VARCHAR},",
            "last_update_uid = #{lastUpdateUid,jdbcType=VARCHAR},",
            "last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP}",
            "where user_account = #{userAccount,jdbcType=VARCHAR}"
    })
    int savePasswordByUserAccountMapper(AdminUsersEntity record);




    ///检测是否登录【根据管理员编号获取状态】
    @Select({
            "select",
            "user_no,user_account,user_status",
            "from admin_users",
            "where user_no = #{userno,jdbcType=VARCHAR} limit 1"
    })
    @Results({
            @Result(column="user_no", property="userNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_account", property="userAccount", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_status", property="userStatus", jdbcType=JdbcType.INTEGER)
    })
    AdminUsersEntity selectCheckLoginByUserNoMapper(@Param("userno") String userno);



    ///获取管理员列表数据条数
    @SelectProvider(type=AdminUsersSqlProvider.class,method = "getUsersListCountSelective")
    Integer getUsersListCountSelective(@Param("paramRequest") PostUsersListRequest usersListRequest);
    ///获取管理员列表数据list
    @SelectProvider(type=AdminUsersSqlProvider.class,method = "getUsersListDataSelective")
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT),
            @Result(column="user_no", property="userNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_account", property="userAccount", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_status", property="userStatus", jdbcType=JdbcType.INTEGER)
    })
    List<AdminUsersEntity> getUsersListDataSelective(@Param("paramRequest") PostUsersListRequest usersListRequest,@Param("StartRow") Integer StartRow,@Param("pageSize") Integer pageSize);



    ///新增管理员
    @Insert({
            "insert into admin_users (`user_no`, `user_account`, ",
            "`user_name`, `user_pwd`,`user_salt`,",
            "`create_uid`,`create_time`) ",
            "values (#{userNo,jdbcType=VARCHAR}, #{userAccount,jdbcType=VARCHAR}, ",
            "#{userName,jdbcType=VARCHAR}, #{userPwd,jdbcType=VARCHAR},#{userSalt,jdbcType=VARCHAR},",
            "#{createUid,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP}) "
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer usersAddMapper(AdminUsersEntity record);



    ///检测账号是否已经存在（根据账号查询返回存在的数据总条数）
    @Select({
            "select",
            "count(1)",
            "from admin_users",
            "where user_account = #{account,jdbcType=VARCHAR} limit 1"
    })
    Integer checkAccountByUserAccountMapper(@Param("account") String account);



    ///管理员状态修改
    ///修改后的状态、当前操作人编号、操作时间、数据id、数据当前状态
    @Update({
            "update admin_users",
            "set user_status = #{userStatus,jdbcType=INTEGER},",
            "last_update_uid = #{lastUpdateUid,jdbcType=VARCHAR},",
            "last_update_time = #{lastUpdateTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=INTEGER}",
            "and user_status = #{nowStatus,jdbcType=INTEGER}",
            "and user_no != #{lastUpdateUid,jdbcType=VARCHAR}"
    })
    Integer saveStatusByIdAndAccountMapper(@Param("userStatus") Integer userStatus, @Param("lastUpdateUid") String lastUpdateUid, @Param("lastUpdateTime") Date lastUpdateTime, @Param("id") Integer id, @Param("nowStatus") Integer nowStatus);



    ///角色操作获取可操作的且状态为有效的管理员list
    ///不包括超级管理员
    @SelectProvider(type=AdminUsersSqlProvider.class,method = "actionUsersListSelective")
    @Results({
            @Result(column="user_no", property="userNo", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR)
    })
    List<AdminUsersEntity> actionUsersListSelective();





}